package com.ny.shiyan;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class shiyan7 {
        private static final String URL = "jdbc:mysql://localhost:3306/usersDb?useSSL=false&serverTimezone=UTC";
        private static final String USER = "root";
        private static final String PASSWORD = "123456";

        static class Employee {
            private int id;
            private String name;
            private String position;
            private double salary;

            public Employee(int id, String name, String position, double salary) {
                this.id = id;
                this.name = name;
                this.position = position;
                this.salary = salary;
            }

            public int getId() { return id; }
            public String getName() { return name; }
            public String getPosition() { return position; }
            public double getSalary() { return salary; }
        }

        public static void main(String[] args) {
            List<Employee> employees = new ArrayList<>();
            employees.add(new Employee(1, "张三", "软件工程师", 15000));
            employees.add(new Employee(2, "李四", "项目经理", 25000));
            employees.add(new Employee(3, "王五", "产品经理", 22000));

            saveEmployeesToDatabase(employees);
        }

        private static void saveEmployeesToDatabase(List<Employee> employees) {
            createEmployeeTableIfNotExists();

            try (Connection conn = DriverManager.getConnection(URL, USER, PASSWORD)) {
                try (Statement stmt = conn.createStatement()) {
                    stmt.executeUpdate("TRUNCATE TABLE employees");
                } catch (SQLException e) {
                    System.out.println("清空表数据时出错（可能是首次运行），继续执行: " + e.getMessage());
                }

                String sql = "INSERT INTO employees (id, name, position, salary) VALUES (?, ?, ?, ?)";
                try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
                    for (Employee emp : employees) {
                        pstmt.setInt(1, emp.getId());
                        pstmt.setString(2, emp.getName());
                        pstmt.setString(3, emp.getPosition());
                        pstmt.setDouble(4, emp.getSalary());
                        pstmt.addBatch();
                    }

                    int[] results = pstmt.executeBatch();
                    System.out.println("成功插入 " + results.length + " 条员工记录");
                }
            } catch (SQLException e) {
                System.out.println("数据库操作错误: " + e.getMessage());
                e.printStackTrace();
            }
        }

        private static void createEmployeeTableIfNotExists() {
            String createTableSQL = "CREATE TABLE IF NOT EXISTS employees ("
                    + "id INT PRIMARY KEY, "
                    + "name VARCHAR(50) NOT NULL, "
                    + "position VARCHAR(50), "
                    + "salary DECIMAL(10, 2)"
                    + ")";

            try (Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
                 Statement stmt = conn.createStatement()) {

                stmt.executeUpdate(createTableSQL);
                System.out.println("员工表已准备就绪");
            } catch (SQLException e) {
                System.out.println("创建表时出错: " + e.getMessage());
                e.printStackTrace();
            }
        }
    }
